package com.masterclient.app.modules.goods.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.eoner.managerlibrary.router.AroutePath;
import com.eoner.baselibrary.bean.goods.CommentData;
import com.eoner.baselibrary.bean.goods.CommentTabModel;
import com.masterclient.app.modules.goods.contract.CommentContract;
import com.masterclient.app.modules.goods.contract.CommentPresenter;
import com.masterclient.app.modules.goods.fragment.CommentFragment;
import com.masterclient.app.modules.timelimited.adapter.ViewPagerAdapter;

import com.eoner.baselib.utils.status.StatusBarUtil;
import com.masterclient.app.widget.TitlebarView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = AroutePath.COMMENT_ACTIVITY)
public class CommentActivity extends BaseActivity<CommentPresenter> implements CommentContract.View{


    @BindView(R.id.title_view)
    TitlebarView title_view;

    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    @Autowired(name = "id")
    String id;



    private List<Fragment> fragments;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.comment_activity);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);

        StatusBarUtil.setStatusBarColor(getActivity(), Color.parseColor("#FFFFFF"));

        init();

        addListener();

        getTab();



    }

    private void addListener() {
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                set_tab(position);
                viewPager.setCurrentItem(position);
            }


            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        title_view.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
    }

    private void getTab() {
        mPresenter.getTabData(id);
    }

    private void init() {
        fragments = new ArrayList<>();
    }


    @OnClick({})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.exchange_btn:

                break;

        }
    }


    private void set_tab(int position) {
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            View view = tabLayout.getTabAt(i).getCustomView();
            if (view == null) {
                return;
            }
            TextView title = (TextView) view.findViewById(R.id.title);

            if (position == i) {
                title.setTextColor(getResources().getColor(R.color.white));
                title.setBackgroundResource(R.drawable.sku_selected);
            } else {
                title.setTextColor(Color.parseColor("#333333"));
                title.setBackgroundResource(R.drawable.bg_rect_tag_black);
            }
        }
    }



    @Override
    public void showGoodsComment(CommentData data) {

    }

    @Override
    public void getCommentListFail(String msg) {

    }

    @Override
    public void getTabDataSuccess(List<CommentTabModel> tabModels) {

        tabLayout.removeAllTabs();

        if(tabModels != null){
            int select = -1;//默认选中的位置
            for (int i = 0; i < tabModels.size(); i++) {
                tabLayout.addTab(tabLayout.newTab());
                TabLayout.Tab tab = tabLayout.getTabAt(i);
                CommentTabModel tabModel = tabModels.get(i);
                if (tab != null) {
                    tab.setCustomView(R.layout.comment_tab_item);
                    View view = tab.getCustomView();
                    TextView title = (TextView) view.findViewById(R.id.title);
                    title.setText(tabModel.getName());
                }
                fragments.add(CommentFragment.getInstance(id,tabModel.getType())) ;
            }
            if (select == -1) {
                select = 0;
            }


            tabLayout.setTabMode (TabLayout.MODE_SCROLLABLE);
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);


            ViewPagerAdapter mainPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),fragments);
            viewPager.setAdapter(mainPagerAdapter);
            //tabLayout.setupWithViewPager(viewPager);
            tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
            viewPager.setOffscreenPageLimit(fragments.size());
            if (tabLayout.getTabAt(select) != null) {
                tabLayout.getTabAt(select).select();
            }
            set_tab(select);
        }else{

        }

    }

    @Override
    public void getTabDataFail(String msg) {
        showToast(msg);
    }
}
